package com.wake.mycomposebook.ui.home.layouts.advlist.swpier

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.MaterialTheme
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Lens
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.wake.carousel.Pager
import com.wake.carousel.PagerState
import com.wake.data.DemoDataProvider
import com.wake.data.model.Item
import com.wake.mycomposebook.utils.showLog

/**
 *@author created by WA03675
 *@date  2022-04-21 16:28
 *@des
 */

@Composable
fun MySwiper2() {
    Column(Modifier.verticalScroll(rememberScrollState())) {
        val items = remember {
            DemoDataProvider.itemList.take(5)
        }
        val pagerState: PagerState = run {
            remember {
                PagerState(1, 0, items.size - 1)
            }
        }

        val selectPage = remember { mutableStateOf(2) }

        PrepareFirstPager(pagerState, items, selectPage)
    }
}

@Composable
fun ColumnScope.PrepareFirstPager(pagerState: PagerState, items: List<Item>, selectPage: MutableState<Int>) {
    Pager(
        state = pagerState,
        modifier = Modifier.height(200.dp)
    ) {
        val item = items[commingPage]
        selectPage.value = pagerState.currentPage
        CarouseItem(item)
    }

    Row(modifier = Modifier.align(alignment = Alignment.CenterHorizontally)) {
        items.forEachIndexed { index, _ ->
            CarouselDot(
                selectPage.value == index,
                Color.Red,
                Icons.Filled.Lens
            )
        }
    }
    Spacer(modifier = Modifier.height(40.dp))
}

@Composable
fun CarouseItem(item: Item) {
    Box {
        Image(
            painter = painterResource(id = item.imageId),
            contentDescription = null,
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .padding(18.dp)
                .fillMaxWidth()
                .height(180.dp)
                .clip(RoundedCornerShape(12.dp))
        )
        Text(
            text = item.title,
            style = MaterialTheme.typography.h6.copy(color = Color.White),
            modifier = Modifier
                .fillMaxWidth()
                .padding(24.dp)
                .align(Alignment.BottomCenter)
        )
    }
}


